package com.tencet.servlet;

import com.tencet.entity.User;
import com.tencet.service.Impl.UserServiceImpl;
import com.tencet.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: aiming    @date: 2022/7/21 21:03
 * @description:
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String phoneNum = req.getParameter("phoneNum");
        String password = req.getParameter("password");

        //调用service
        UserService userService = new UserServiceImpl();

        //用户一旦登陆,后续要怎么判断用户是否登录
        //通过判断session中是否有user对象
        User user = userService.login(phoneNum,password);

        boolean result = false;
        if (user != null){
            result = true;
            //将user对象存入session中,供后期判断是否登录使用
            req.getSession().setAttribute("user",user);
            req.getSession().setAttribute("phone", user.getPhone_num());
        }

        resp.getWriter().write(""+result);

    }
}
